/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div>
    <div>
      <div class="fixed-bar">
        <div class="item-title">
          <router-link
            class="back"
            :to="{name: 'store_grades', params:{currentPage:$route.params.currentPage}}"
            title="返回列表"
          >
            <i class="fa fa-arrow-circle-o-left"></i>
          </router-link>
          <div class="subject">
            <h3>
              店铺等级管理
              -
              <span v-if="sgId">编辑</span>
              <span v-else>新增</span>店铺店铺
            </h3>
            <h5>网站系统店铺等级管理</h5>
          </div>
        </div>
      </div>
      <el-form ref="form" :model="form" label-width="200px">
        <el-form-item label="等级名称" required>
          <el-input v-model="form.sg_name"></el-input>
          <p class="notic">设置店铺等级名称</p>
          <p class="err-msg" v-if="errResult.sg_name && !form.sg_name">{{errResult.sg_name}}</p>
        </el-form-item>
        <el-form-item label="可发布商品数">
          <el-input v-model="form.sg_goods_limit"></el-input>
          <p class="notic">0表示没有限制</p>
        </el-form-item>
        <el-form-item label="可上传图片数">
          <el-input v-model="form.sg_album_limit"></el-input>
          <p class="notic">0表示没有限制</p>
        </el-form-item>
        <el-form-item label="可选模板套数">
          <el-input v-model="form.sg_template_limit"></el-input>
          <p class="notic">0表示没有限制</p>
        </el-form-item>
        <el-form-item label="收费标准">
          <el-input v-model="form.sg_price"></el-input>
          <p class="notic">收费标准，单：元/年，必须为数字，在会员开通或升级店铺时将显示在前台</p>
        </el-form-item>
        <el-form-item label="级别">
          <el-input v-model="form.sg_sort"></el-input>
          <p class="notic">数值越大表明级别越高</p>
        </el-form-item>
        <el-form-item label="申请说明">
          <el-input type="textarea" v-model="form.sg_description" style="width:350px"></el-input>
          <p class="notic">申请说明，在会员开通或升级店铺时将显示在前台</p>
        </el-form-item>
        <!-- <el-form-item label="权限分配" required>开发中...</el-form-item> -->
        <el-form-item>
          <el-button type="primary" @click="onSubmit" :loading="loading">确认提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getStoreGrade, updateStoreGrade, addStoreGrade } from "@/utils/api";
export default {
  data() {
    return {
      form: {},
      sgId: this.$route.params.sgId,
      loading: false,
      errResult: {}
    };
  },
  created() {
    if (this.$route.params.sgId) {
      this.getStoreGrade();
    }
  },
  methods: {
    getStoreGrade() {
      var that = this;
      getStoreGrade({
        sg_id: that.sgId
      }).then(function(res) {
        res.sg_price = res.sg_price.toFixed(2);
        that.form = res;
      });
    },
    onSubmit() {
      this.loading = true;
      var that = this;
      if (this.form.sg_id) {
        //编辑
        updateStoreGrade(that.form).then(function(res) {
          if (res.status == 1) {
            that.$message({
              message: "保存成功",
              type: "success",
              onClose: function() {
                that.$router.push({ name: "store_grades" });
              }
            });
          } else if (res.status == -1) {
            that.$message.error("参数错误");
            that.errResult = res.result;
          } else {
            that.$message.error(res.msg);
          }
          that.loading = false;
        });
      } else {
        //添加
        addStoreGrade(that.form).then(function(res) {
          if (res.status == 1) {
            that.$message.success({
              message: "添加成功",
              duration: 1000,
              onClose: function() {
                that.$router.push({ name: "store_grades" });
              }
            });
          } else if (res.status == -1) {
            that.$message.error("参数错误");
            that.errResult = res.result;
          } else {
            that.$message.error(res.msg);
          }
          that.loading = false;
        });
      }
    }
  }
};
</script>

<style scoped>
</style>
